﻿using MenuItem = OMC.DataManagement.MenuItem;
using Folder = OMC.DataManagement.Folder;
using System.Collections.Generic;

namespace OMC.Classification
{
    /// <summary>
    /// This class could manage all about the Prediction of the Result
    /// of a classification.
    /// </summary>
    public class Prediction
    {
        private double[] result;
        private TextClassifier textClassifier;
        private const int maxNumberOfFolders = 7;

        /// <summary>
        /// The constructor to create a Prediction object.
        /// </summary>
        /// <param name="textClassifier">The TextClassifier</param>
        /// <param name="result">a double Array of Results</param>
        public Prediction(TextClassifier textClassifier, double[] result)
        {
            this.result = result;
            this.textClassifier = textClassifier;
        }

        /// <summary>
        /// This method makes a prediction, which classes passes best.
        /// </summary>
        /// <param name="folderList">a referenced list of the folderlist</param>
        /// <returns>a list of the predicted menu items</returns>
        public List<MenuItem> GetPredictedButtonList(List<Folder> folderList)
        {
            List<MenuItem> buttonList = new List<MenuItem>();
            for (int i = 0; i < result.Length; i++)
            {
                string folderName = null;
                string folderExtra = null;
                foreach (Folder folder in folderList)
                {
                    if (folder.FolderInfo == textClassifier.GetClassName(i))
                    {
                        folderName = folder.FolderName;
                        folderExtra = folder.FolderExtra;
                    }
                }
                buttonList.Add(new MenuItem(folderName, folderExtra, (result[i] * 100)));
            }

            buttonList.Sort(delegate(MenuItem item1, MenuItem item2) { return item1.Probability.CompareTo(item2.Probability); });
            buttonList.Reverse();
            buttonList.TrimExcess();
            if (buttonList.Capacity > maxNumberOfFolders)
            {
                buttonList.RemoveRange(maxNumberOfFolders - 1, buttonList.Capacity - maxNumberOfFolders);
            }
            return buttonList;
        }
    }
}